<template>
  <div class="user-status">
    <div v-if="!authStore.isAuthenticated" class="auth-section">
      <button class="btn btn-primary" @click="showAuthModal = true">
        登录 / 注册
      </button>
    </div>

    <div v-else class="user-section">
      <div class="user-info">
        <div class="user-avatar">
          <span>{{ userInitials }}</span>
        </div>
        <div class="user-details">
          <div class="user-name">{{ authStore.user?.nickname || authStore.user?.username }}</div>
          <div class="user-email">{{ authStore.user?.email }}</div>
        </div>
      </div>

      <div class="user-actions">
        <button class="btn-icon" @click="showProfileModal = true" title="个人资料">
          <span>⚙️</span>
        </button>
        <button class="btn-icon" @click="showScoresModal = true" title="游戏记录">
          <span>📊</span>
        </button>
        <button class="btn-icon" @click="handleLogout" title="退出登录">
          <span>🚪</span>
        </button>
      </div>
    </div>

    <!-- 认证模态框 -->
    <AuthModal :show="showAuthModal" @close="showAuthModal = false" />

    <!-- 个人资料模态框 -->
    <ProfileModal v-if="showProfileModal" @close="showProfileModal = false" />

    <!-- 游戏记录模态框 -->
    <ScoresModal v-if="showScoresModal" @close="showScoresModal = false" />
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
import { useAuthStore } from '@/stores/auth';
import AuthModal from './AuthModal.vue';
import ProfileModal from './ProfileModal.vue';
import ScoresModal from './ScoresModal.vue';

const authStore = useAuthStore();

const showAuthModal = ref(false);
const showProfileModal = ref(false);
const showScoresModal = ref(false);

const userInitials = computed(() => {
  const user = authStore.user;
  if (!user) return '';
  
  const name = user.nickname || user.username;
  return name.substring(0, 2).toUpperCase();
});

function handleLogout() {
  if (confirm('确定要退出登录吗？')) {
    authStore.logout();
  }
}
</script>

<style lang="scss" scoped>
.user-status {
  display: flex;
  align-items: center;
  gap: 15px;
}

.auth-section {
  .btn {
    padding: 8px 20px;
  }
}

.user-section {
  display: flex;
  align-items: center;
  gap: 15px;
  padding: 10px 20px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 25px;
  backdrop-filter: blur(10px);
}

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: bold;
  font-size: 0.9rem;
}

.user-details {
  color: white;

  .user-name {
    font-weight: bold;
    font-size: 0.9rem;
  }

  .user-email {
    font-size: 0.75rem;
    opacity: 0.8;
  }
}

.user-actions {
  display: flex;
  gap: 8px;
}

.btn-icon {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.2);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  transition: all 0.2s;

  &:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: scale(1.1);
  }

  span {
    font-size: 0.9rem;
  }
}
</style>
